//
//  ZhanTiGunDongCollectionViewCell.m
//  礼物说
//
//  Created by dllo on 16/4/22.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "ZhanTiGunDongCollectionViewCell.h"

@interface ZhanTiGunDongCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *zhongZhuanArr;

@end

@implementation ZhanTiGunDongCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)dealloc {
    [_modelArr release];
    [_zhongZhuanArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)setModelArr:(NSMutableArray *)modelArr {
    if (_modelArr != modelArr) {
        [_modelArr release];
        _modelArr = [modelArr retain];
    }
    self.zhongZhuanArr = [NSMutableArray arrayWithArray:_modelArr];
}

- (void)createCell {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake(kWIDTHS(100), kHEIGHTS(100));
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHS(375), self.contentView.frame.size.height) collectionViewLayout:layout];
    [self.contentView addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.bounces = YES;
    _collectionView.pagingEnabled = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[ZhuanTiGunDongCellCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _zhongZhuanArr.count - 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZhuanTiGunDongCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _zhongZhuanArr[indexPath.row + 1];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZhuanTiModels *model = _zhongZhuanArr[indexPath.row + 1];
    NSArray *arr = [model.target_url componentsSeparatedByString:@"&"];
    if ([arr[0] containsString:@"url"]) {

    }else if ([arr[0] containsString:@"post"]) {
        NSString *number = [arr[1] substringFromIndex:8];
        UIViewController *viewController = [self viewController];
        
        GongLvXiangQingViewController * gongLvVc = [[GongLvXiangQingViewController alloc] init];
        
        gongLvVc.number = (NSNumber *)number;
        
        [viewController.navigationController pushViewController:gongLvVc animated:YES];
        [gongLvVc release];
        
    }else {
        NSString * number = [arr[1] substringFromIndex:9];
        
        ClassifyGongLvViewController * classifyVc = [[ClassifyGongLvViewController alloc]init];
        
        UIViewController * ViewController = [self viewController];
        classifyVc.num = (NSNumber *)number;
        classifyVc.Judge = @"zhuanTi";
        classifyVc.titles = @"";
        
        [ViewController.navigationController pushViewController:classifyVc animated:YES];
        [classifyVc release];
    }
}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


@end
